/*
 * ModoForm.java
 *
 * Created on 1 de julio de 2008, 20:46
 */

package View;

import Model.Congreso;
import Model.Usuario;
import Servicio.Proxy;
import com.birosoft.liquid.LiquidLookAndFeel;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

/**
 *
 * @author  Gigin
 */
public class ModoForm extends javax.swing.JFrame {
    private ArrayList<Congreso> mCongresos;
    private PpalForm ppal;
    private javax.swing.JFrame fr;
    private String estado;
    
    
    public ModoForm() {
       
    }

    /** Creates new form ModoForm */
    public ModoForm(javax.swing.JFrame frame, String estado){
       
        this.fr = frame;
        this.setAlwaysOnTop(true);
        this.estado =estado;
        iniciarPantalla();
        
    }
    
    private void iniciarPantalla(){
    // Se carga el Look and Feel
        try {
            UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");   
        } catch (Exception e) { }
        LiquidLookAndFeel.setStipples(false);
        
        
        initComponents();
       
        cargarComboCongresos();
        jComboCongresos.setEnabled(false);
        jLabelUsuario.setEnabled(false);
        jTextUsuario.setEnabled(false);
        jLabelPass.setEnabled(false);
        jTextPass.setEnabled(false);
        jButtonAceptar.setEnabled(false);
        if (estado.compareTo("") == 0)
            jButton1.setEnabled(false);
        else
            jButton1.setEnabled(true);
    
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jCheckCongreso = new javax.swing.JCheckBox();
        jCheckOficina = new javax.swing.JCheckBox();
        jComboCongresos = new javax.swing.JComboBox();
        jButtonAceptar = new javax.swing.JButton();
        jLabelUsuario = new javax.swing.JLabel();
        jLabelPass = new javax.swing.JLabel();
        jTextUsuario = new javax.swing.JTextField();
        jTextPass = new javax.swing.JPasswordField();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Monotype Corsiva", 1, 24));
        jLabel1.setForeground(new java.awt.Color(94, 148, 189));
        jLabel1.setText("Seleccionar modo de operación");

        jCheckCongreso.setFont(new java.awt.Font("Verdana", 1, 10));
        jCheckCongreso.setText("Modo Congreso");
        jCheckCongreso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckCongresoActionPerformed(evt);
            }
        });

        jCheckOficina.setFont(new java.awt.Font("Verdana", 1, 10));
        jCheckOficina.setText("Modo Oficina");
        jCheckOficina.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckOficinaActionPerformed(evt);
            }
        });

        jComboCongresos.setFont(new java.awt.Font("Verdana", 0, 10));
        jComboCongresos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboCongresosActionPerformed(evt);
            }
        });

        jButtonAceptar.setFont(new java.awt.Font("Verdana", 1, 10)); // NOI18N
        jButtonAceptar.setForeground(new java.awt.Color(94, 148, 189));
        jButtonAceptar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Botones/Aceptar_normal.gif"))); // NOI18N
        jButtonAceptar.setToolTipText("Ingresar con el modo seleccionado");
        jButtonAceptar.setBorderPainted(false);
        jButtonAceptar.setContentAreaFilled(false);
        jButtonAceptar.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/Botones/Aceptar_deshabilitado.gif"))); // NOI18N
        jButtonAceptar.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/Botones/Aceptar_presionado.gif"))); // NOI18N
        jButtonAceptar.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/Botones/Aceptar_mouse.gif"))); // NOI18N
        jButtonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAceptarActionPerformed(evt);
            }
        });

        jLabelUsuario.setText("Usuario:");

        jLabelPass.setText("Contraseña:");

        jTextPass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextPassActionPerformed(evt);
            }
        });

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Botones/Cancelar_normal.gif"))); // NOI18N
        jButton1.setToolTipText("Cancelar");
        jButton1.setBorderPainted(false);
        jButton1.setContentAreaFilled(false);
        jButton1.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/Botones/Cancelar_deshabilitado.gif"))); // NOI18N
        jButton1.setPressedIcon(new javax.swing.ImageIcon(getClass().getResource("/Botones/Cancelar_presionado.gif"))); // NOI18N
        jButton1.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/Botones/Cancelar_mouse.gif"))); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(75, 75, 75)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jCheckOficina)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(21, 21, 21)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabelPass)
                                    .addComponent(jLabelUsuario))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jTextPass)
                                    .addComponent(jTextUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(jComboCongresos, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jCheckCongreso)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jButtonAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(35, 35, 35)
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addComponent(jLabel1)))
                .addContainerGap(56, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel1)
                .addGap(27, 27, 27)
                .addComponent(jCheckOficina)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelUsuario))
                .addGap(12, 12, 12)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelPass)
                    .addComponent(jTextPass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21)
                .addComponent(jCheckCongreso)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jComboCongresos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonAceptar, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jCheckOficinaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckOficinaActionPerformed
    if (jCheckOficina.isSelected()){
        jCheckCongreso.setSelected(false);
        jComboCongresos.setEnabled(false);
        jLabelUsuario.setEnabled(true);
        jTextUsuario.setEnabled(true);
        jLabelPass.setEnabled(true);
        jTextPass.setEnabled(true);
        jButtonAceptar.setEnabled(true);
        jTextUsuario.grabFocus();
        jTextUsuario.setCaretPosition(0);
                
                
    }else
    {
        jLabelUsuario.setEnabled(false);
        jTextUsuario.setEnabled(false);
        jLabelPass.setEnabled(false);
        jTextPass.setEnabled(false);
        jButtonAceptar.setEnabled(false);
    
    }
}//GEN-LAST:event_jCheckOficinaActionPerformed

private void jCheckCongresoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckCongresoActionPerformed
   if(jCheckCongreso.isSelected()){
        jCheckOficina.setSelected(false);
        jLabelUsuario.setEnabled(false);
        jTextUsuario.setEnabled(false);
        jLabelPass.setEnabled(false);
        jTextPass.setEnabled(false);
        jComboCongresos.setEnabled(true);
        jButtonAceptar.setEnabled(true);
   }else{
        jLabelUsuario.setEnabled(true);
        jTextUsuario.setEnabled(true);
        jLabelPass.setEnabled(true);
        jTextPass.setEnabled(true);
        jComboCongresos.setEnabled(false);
        jButtonAceptar.setEnabled(false);
   }
        
}//GEN-LAST:event_jCheckCongresoActionPerformed

private void jComboCongresosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboCongresosActionPerformed
    
}//GEN-LAST:event_jComboCongresosActionPerformed

private void jButtonAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAceptarActionPerformed
    String modo;
    Congreso cong;
    Usuario usr;
    
    if (jCheckOficina.isSelected()){
        modo = "Oficina";
        cong = null;
        try{
            usr = Proxy.validarUsuario(jTextUsuario.getText(), String.valueOf(jTextPass.getPassword()));
            if (usr == null){
                this.setAlwaysOnTop(false);
                JOptionPane.showMessageDialog(new JFrame(), "El usuario no existe o su contraseña es incorrecta", "Atención", JOptionPane.WARNING_MESSAGE);
                jTextPass.setText("");
                jTextUsuario.setText("");
                jTextUsuario.grabFocus();
                jTextUsuario.setCaretPosition(0);
            }else{
                   // Proxy.inicializarProxy();
                    ppal.estadoPanel(true,true);
                    ppal.cargarFormulario(modo,cong,usr);
                    this.dispose();
            }
            }catch (Exception e){
                    usr = null;
                    JOptionPane.showMessageDialog(new JFrame(), "El usuario no existe o su contraseña es incorrecta", "Atención", JOptionPane.WARNING_MESSAGE);
        }
        
        
    }else if (jCheckCongreso.isSelected()){
        modo = "Congreso";
        cong = (Congreso)jComboCongresos.getSelectedItem();
        usr = null;
        //Proxy.inicializarProxy();
        ppal.estadoPanel(true,false);
        ppal.cargarFormulario(modo,cong,usr);
        this.dispose();
      }
    
    
}//GEN-LAST:event_jButtonAceptarActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    if (estado.compareTo("Oficina") == 0)
        ppal.estadoPanel(true, true);
    else
        ppal.estadoPanel(true, false);
    this.dispose();
}//GEN-LAST:event_jButton1ActionPerformed

private void jTextPassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextPassActionPerformed
this.jButtonAceptarActionPerformed(evt);
}//GEN-LAST:event_jTextPassActionPerformed

   /** Carga el combo de Congresos */
    public void cargarComboCongresos(){
        this.setCongresos(Proxy.obtenerCongresos());
       
        for (int i=0 ; i < this.mCongresos.size()-1 ; i++)
             if (mCongresos.get(i).getEstado().compareToIgnoreCase("Activo")==0) jComboCongresos.addItem(this.mCongresos.get(i));  
    }

    public ArrayList<Congreso> getCongresos () {
        return mCongresos;
    }
 
    public void setCongresos (ArrayList<Congreso> val) {
        this.mCongresos = val;
    }
    
    public void setPpal(PpalForm p){
        this.ppal=p;
}
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButtonAceptar;
    private javax.swing.JCheckBox jCheckCongreso;
    private javax.swing.JCheckBox jCheckOficina;
    private javax.swing.JComboBox jComboCongresos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelPass;
    private javax.swing.JLabel jLabelUsuario;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPasswordField jTextPass;
    private javax.swing.JTextField jTextUsuario;
    // End of variables declaration//GEN-END:variables

}
